*----------------------------------------------------------------------*
* 程序的事务代码: ZFIU023
* 程序名称:  进项发票/海关缴款书数据从诺诺平台获取
* 作者       :  王鹏
* 时间        : 20241130
* 模块      : FI
* 负责人 :
* 描述 :
*
*
*
* 特殊特性:
*
*
*----------------------------------------------------------------------*
* 修改记录:
*作者      时间      描述
*-----------  --------  -----------------------------------------------*
*
*----------------------------------------------------------------------*
REPORT ZFIU023.
TABLES:ZFIT078_HEAD.

DATA:
  GV_ERROR           TYPE C,
  GT_INVOICE_LIST_HG TYPE ZCL_DT_QUERY_COLLECT_CUST_TAB1,
  GT_INVOICE_LIST    TYPE ZCLDT_INPUT_INVOICE_LIST_Q_TAB.

DATA:GO_TAX    TYPE REF TO ZFIDL004.

CONSTANTS:
  GC_CONCATE_CHAR TYPE C VALUE ':'.

SELECT-OPTIONS:
  S_ZFPHM FOR ZFIT078_HEAD-ZFPHM  NO INTERVALS,
  S_DATE FOR SY-DATUM NO-EXTENSION OBLIGATORY.

PARAMETERS:
  P_INV   AS CHECKBOX DEFAULT 'X',
  P_HGINV AS CHECKBOX.

INITIALIZATION.
  PERFORM FRM_INIT_DATA.

AT SELECTION-SCREEN.

START-OF-SELECTION.
  PERFORM FRM_GET_INV_LIST.

  PERFORM FRM_GET_INV_DETAIL.


*&---------------------------------------------------------------------*
*& Form frm_init_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_INIT_DATA .
  REFRESH S_DATE[].
  FREE GO_TAX.

*  CALL FUNCTION 'RE_ADD_MONTH_TO_DATE'
*    EXPORTING
*      MONTHS  = '1-'
*      OLDDATE = SY-DATUM   "lv_old_date
*    IMPORTING
*      NEWDATE = S_DATE-LOW.
*  S_DATE-LOW+6(2) = '01'.

  S_DATE-LOW = SY-DATUM - 3.
  S_DATE-HIGH = SY-DATUM - 1.

  APPEND S_DATE.
  CREATE OBJECT GO_TAX.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_INV_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_GET_INV_LIST .
  DATA:
    LS_MSG             TYPE BAPIRET2,
    LT_INVOICE_LIST_HG TYPE ZCL_DT_QUERY_COLLECT_CUST_TAB1,
    LT_INVOICE_LIST    TYPE ZCLDT_INPUT_INVOICE_LIST_Q_TAB.

  REFRESH:
    GT_INVOICE_LIST[],
    GT_INVOICE_LIST_HG[].

  IF P_INV IS INITIAL AND P_HGINV IS INITIAL.
    GV_ERROR = 'X'.
    WRITE: / '发票或者海关缴款书至少选择一项'(T10).
    RETURN.
  ENDIF.

  IF S_DATE-HIGH IS INITIAL.
    S_DATE-HIGH = S_DATE-LOW.
  ENDIF.

  IF S_ZFPHM[] IS NOT INITIAL.
    LOOP AT S_ZFPHM.

      IF P_INV IS NOT INITIAL.
        REFRESH LT_INVOICE_LIST[].
        CALL FUNCTION 'ZFIFM_INV_GET_LIST'
          EXPORTING
            IV_ZYSFPHM          = S_ZFPHM-LOW
            IV_BEGDA            = S_DATE-LOW
            IV_ENDDA            = S_DATE-HIGH
            IV_ENTER_ACCOUNT_NO = SPACE       "输入发票号码时，不检查入账状态
          IMPORTING
            ES_MSG              = LS_MSG
            ET_INV_LIST         = LT_INVOICE_LIST.

        IF LS_MSG-TYPE = 'E'.
          GV_ERROR = 'X'.
          WRITE: / '获取发票数据失败，原因：'(T08), LS_MSG-MESSAGE.
          EXIT.
        ELSE.
          APPEND LINES OF LT_INVOICE_LIST TO GT_INVOICE_LIST.
        ENDIF.
      ENDIF.

      IF P_HGINV IS NOT INITIAL.
        REFRESH LT_INVOICE_LIST_HG[].
        CALL FUNCTION 'ZFIFM_INV_HG_GET_LIST_V1'
          EXPORTING
            IV_ZYSFPHM     = S_ZFPHM-LOW
            IV_BEGDA       = S_DATE-LOW
            IV_ENDDA       = S_DATE-HIGH
          IMPORTING
            ES_MSG         = LS_MSG
            ET_INV_HG_LIST = LT_INVOICE_LIST_HG.

        IF LS_MSG-TYPE = 'E'.
          GV_ERROR = 'X'.
          WRITE: / '获取海关缴款书失败，原因：'(T09), LS_MSG-MESSAGE.
          EXIT.
        ELSE.
          APPEND LINES OF LT_INVOICE_LIST_HG TO GT_INVOICE_LIST_HG.
        ENDIF.
      ENDIF.
    ENDLOOP.

    IF GV_ERROR IS NOT INITIAL.
      RETURN.
    ENDIF.

  ELSE.

    IF P_INV IS NOT INITIAL.
      REFRESH LT_INVOICE_LIST[].
      CALL FUNCTION 'ZFIFM_INV_GET_LIST'
        EXPORTING
          "  IV_ZYSFPHM  = S_ZFPHM-LOW
          IV_BEGDA            = S_DATE-LOW
          IV_ENDDA            = S_DATE-HIGH
          IV_ENTER_ACCOUNT_NO = 'X'   "只输入期间的话，只取未入账发票
        IMPORTING
          ES_MSG              = LS_MSG
          ET_INV_LIST         = LT_INVOICE_LIST.

      IF LS_MSG-TYPE = 'E'.
        GV_ERROR = 'X'.
        WRITE: / '获取发票数据失败，原因：'(T08), LS_MSG-MESSAGE.
        EXIT.
      ELSE.
        APPEND LINES OF LT_INVOICE_LIST TO GT_INVOICE_LIST.
      ENDIF.
    ENDIF.

    IF P_HGINV IS NOT INITIAL.
      REFRESH LT_INVOICE_LIST_HG[].
      CALL FUNCTION 'ZFIFM_INV_HG_GET_LIST_V1'
        EXPORTING
*         IV_ZYSFPHM     = S_ZFPHM-LOW
          IV_BEGDA       = S_DATE-LOW
          IV_ENDDA       = S_DATE-HIGH
        IMPORTING
          ES_MSG         = LS_MSG
          ET_INV_HG_LIST = LT_INVOICE_LIST_HG.

      IF LS_MSG-TYPE = 'E'.
        GV_ERROR = 'X'.
        WRITE: / '获取海关缴款书失败，原因：'(T09), LS_MSG-MESSAGE.
        EXIT.
      ELSE.
        APPEND LINES OF LT_INVOICE_LIST_HG TO GT_INVOICE_LIST_HG.
      ENDIF.
    ENDIF.

  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_INV_detail
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_GET_INV_DETAIL .

  CHECK GV_ERROR IS INITIAL.

  DATA:
    "LS_DATA         TYPE ZFIS006_RIGHT,
    LV_TABIX        TYPE I,
    LV_STR          TYPE STRING,
    LV_STR1         TYPE STRING,
    LS_MSG          TYPE BAPIRET2,
    LS_ZFIT078_HIS  TYPE ZFIT078_HIS,
    LT_ZFIT078_HIS  TYPE STANDARD TABLE OF ZFIT078_HIS,
    LS_ZFIT078_HEAD TYPE ZFIT078_HEAD,
    LT_ZFIT078_HEAD TYPE STANDARD TABLE OF ZFIT078_HEAD.

  DATA:
    LT_ITEM TYPE ZCLDT_NTSINPUT_INVOICE_ITE_TAB,
    LS_ITEM TYPE ZCLDT_NTSINPUT_INVOICE_ITEM_QU,
    LT_TEMP TYPE ZCLDT_NTSINPUT_INVOICE_ITE_TAB,
    LS_TEMP TYPE ZCLDT_NTSINPUT_INVOICE_ITEM_QU.


  DATA:
    LS_CONTENT TYPE ZCLDT_NTSINPUT_INVOICE_ITEM_Q3,
    LS_PARA    TYPE ZCLDT_NTSINPUT_INVOICE_ITEM_Q1,
    LV_COUNT   TYPE I,
    LV_MAX     TYPE I VALUE '100',
    LV_BANKN   TYPE LFBK-BANKN,
    LV_BKREF   TYPE LFBK-BKREF.

*&--------------调用接口获取发票明细
  CLEAR:LT_ITEM,LV_COUNT.
  LOOP AT GT_INVOICE_LIST INTO DATA(LS_INVOICES).
    LV_COUNT = LV_COUNT + 1.

    CLEAR:LS_PARA.
    IF LS_INVOICES-ELEC_INVOICE_NUMBER IS NOT INITIAL.
      LS_PARA-INVOICE_NO = LS_INVOICES-ELEC_INVOICE_NUMBER.
    ELSE.
      LS_PARA-INVOICE_NO    = LS_INVOICES-INVOICE_NO. " 发票号码
      LS_PARA-INVOICE_CODE  = LS_INVOICES-INVOICE_CODE." 发票代码(纸质发票必传)
    ENDIF.
    APPEND LS_PARA TO LS_CONTENT-PARAM_LIST.

    IF LV_COUNT MOD LV_MAX = 0 OR LV_COUNT = LINES( GT_INVOICE_LIST ). "数据量100 或者最后

      CLEAR LS_MSG.
      REFRESH LT_TEMP[].
      CALL FUNCTION 'ZFIFM_INV_GET_LIST_DETAIL'
        EXPORTING
          IS_CONTENT  = LS_CONTENT
        IMPORTING
          ES_MSG      = LS_MSG
          ET_INV_LIST = LT_TEMP.

      IF LS_MSG-TYPE = 'E'.
        GV_ERROR = 'X'.
        WRITE: / LS_MSG-MESSAGE.
        EXIT.
      ENDIF.

      APPEND LINES OF LT_TEMP TO LT_ITEM.
      CLEAR LS_CONTENT.
    ENDIF.
  ENDLOOP.

  IF GV_ERROR IS NOT INITIAL.
    RETURN.
  ENDIF.

  LOOP AT GT_INVOICE_LIST INTO LS_INVOICES.

    CLEAR LS_ZFIT078_HEAD.
    IF LS_INVOICES-ELEC_INVOICE_NUMBER IS NOT INITIAL.
      LS_ZFIT078_HEAD-ZFPHM = LS_INVOICES-ELEC_INVOICE_NUMBER.
    ELSE.
      LS_ZFIT078_HEAD-ZFPHM = LS_INVOICES-INVOICE_CODE && '-' && LS_INVOICES-INVOICE_NO.
    ENDIF.
    LS_ZFIT078_HEAD-PDF_URL = LS_INVOICES-BACKUP_URL.
    LS_ZFIT078_HEAD-XML_URL = LS_INVOICES-XML_URL.

    " 开票时间
    LS_ZFIT078_HEAD-ZKPSJ = LS_INVOICES-INVOICE_TIME+0(4) &&
                         LS_INVOICES-INVOICE_TIME+5(2) &&
                         LS_INVOICES-INVOICE_TIME+8(2).

    "销方名称(供应商描述)
    LS_ZFIT078_HEAD-ZXFMC = LS_INVOICES-SELLER_TAX_NAME.
    " 销方税号
    LS_ZFIT078_HEAD-ZXFSH = LS_INVOICES-SELLER_TAX_NO.

    "购方名称(公司描述)
    LS_ZFIT078_HEAD-ZGFMC = LS_INVOICES-PAYER_NAME.
    " 购方税号
    LS_ZFIT078_HEAD-ZGFSH = LS_INVOICES-PAYER_TAX_NO.

    " 含税总金额、不含税总金额、合计税额
    TRY .
        LS_ZFIT078_HEAD-ZHSJE  = LS_INVOICES-TOTAL_AMOUNT.
        LS_ZFIT078_HEAD-ZBHSJE = LS_INVOICES-EX_TAX_AMOUNT.
        LS_ZFIT078_HEAD-ZHJSE  = LS_INVOICES-TAX_AMOUNT.
      CATCH CX_ROOT.

    ENDTRY.

    IF LS_INVOICES-ELEC_INVOICE_NUMBER IS NOT INITIAL.
      READ TABLE LT_ITEM INTO LS_ITEM WITH KEY ELEC_INVOICE_NUMBER = LS_INVOICES-ELEC_INVOICE_NUMBER.
      IF SY-SUBRC = 0.
        " 发票流水号
        LS_ZFIT078_HEAD-ZFPLS = LS_ITEM-SERIAL_NO.
        " 开票备注
        LS_ZFIT078_HEAD-ZKPBZ = LS_ITEM-REMARK.
        " 销方银行账号
        LS_ZFIT078_HEAD-ZXFZH = LS_ITEM-SELLER_ACCOUNT.
      ENDIF.

    ELSE.
      READ TABLE LT_ITEM INTO LS_ITEM WITH KEY INVOICE_CODE = LS_INVOICES-INVOICE_CODE
                                               INVOICE_NO   = LS_INVOICES-INVOICE_NO.
      IF SY-SUBRC = 0.
        " 发票流水号
        LS_ZFIT078_HEAD-ZFPLS = LS_ITEM-SERIAL_NO.
        " 开票备注
        LS_ZFIT078_HEAD-ZKPBZ = LS_ITEM-REMARK.
        " 销方银行账号
        LS_ZFIT078_HEAD-ZXFZH = LS_ITEM-SELLER_ACCOUNT.
      ENDIF.
    ENDIF.

    CONDENSE LS_INVOICES-INVOICE_LINE.
    CASE LS_INVOICES-INVOICE_LINE.
      WHEN 'p'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '普通发票(电子)'.
      WHEN 'c'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '普通发票(纸质)'.
      WHEN 's'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '专用发票(纸质)'.
      WHEN 'b'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '专用发票(电子)'.

      WHEN 'e'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '收购发票(电子)'.
      WHEN 'f'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '收购发票(纸质)'.
      WHEN 't'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '增值税电子通行费发票'.
      WHEN 'j'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '机动车销售统一发票'.
      WHEN 'u'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '二手车发票'.

      WHEN 'bs'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '电子发票（增值税专用发票）'.
      WHEN 'pc'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '电子发票（增值税普通发票）'.
      WHEN 'es'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '全电纸质发票(增值税专用发票)--即全电专票(纸质)'.
      WHEN 'ec'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '全电纸质发票(普通发票)--即全电普票(纸质)'.

      WHEN 'hk'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '数电票（航空）'.
      WHEN 'tl'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '(数电铁路票)'.
      WHEN 'dj'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '数电机动车发票(电子) '.
      WHEN 'sj'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '数电机动车发票(纸质)'.
      WHEN 'du'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '数电二手车发票(电子) '.
      WHEN 'su'.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE && GC_CONCATE_CHAR && '数电二手车发票(纸质)'.

      WHEN OTHERS.
        LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICES-INVOICE_LINE.
    ENDCASE.


    CONDENSE LS_INVOICES-INVOICE_STATUS.
    CASE LS_INVOICES-INVOICE_STATUS.
      WHEN '1'.
        LS_ZFIT078_HEAD-INVOICE_STATUS = LS_INVOICES-INVOICE_STATUS && GC_CONCATE_CHAR && '正常'.
      WHEN '2'.
        LS_ZFIT078_HEAD-INVOICE_STATUS = LS_INVOICES-INVOICE_STATUS && GC_CONCATE_CHAR && '红冲'.
      WHEN '3'.
        LS_ZFIT078_HEAD-INVOICE_STATUS = LS_INVOICES-INVOICE_STATUS && GC_CONCATE_CHAR && '作废'.
      WHEN '4'.
        LS_ZFIT078_HEAD-INVOICE_STATUS = LS_INVOICES-INVOICE_STATUS && GC_CONCATE_CHAR && '失控'.
      WHEN '5'.
        LS_ZFIT078_HEAD-INVOICE_STATUS = LS_INVOICES-INVOICE_STATUS && GC_CONCATE_CHAR && '异常'.
      WHEN '6'.
        LS_ZFIT078_HEAD-INVOICE_STATUS = LS_INVOICES-INVOICE_STATUS && GC_CONCATE_CHAR && '认证异常'.
      WHEN '7'.
        LS_ZFIT078_HEAD-INVOICE_STATUS = LS_INVOICES-INVOICE_STATUS && GC_CONCATE_CHAR && '红字待确认'.
      WHEN '8'.
        LS_ZFIT078_HEAD-INVOICE_STATUS = LS_INVOICES-INVOICE_STATUS && GC_CONCATE_CHAR && '已部分冲红'.
      WHEN '9'.
        LS_ZFIT078_HEAD-INVOICE_STATUS = LS_INVOICES-INVOICE_STATUS && GC_CONCATE_CHAR && '已全部冲红'.
      WHEN OTHERS.
        LS_ZFIT078_HEAD-INVOICE_STATUS = LS_INVOICES-INVOICE_STATUS.
    ENDCASE.

    CONDENSE LS_INVOICES-INVOICE_TYPE.
    CASE LS_INVOICES-INVOICE_TYPE.
      WHEN '1'.
        LS_ZFIT078_HEAD-INVOICE_TYPE = LS_INVOICES-INVOICE_TYPE && GC_CONCATE_CHAR && '蓝票'.
      WHEN '2'.
        LS_ZFIT078_HEAD-INVOICE_TYPE = LS_INVOICES-INVOICE_TYPE && GC_CONCATE_CHAR && '红票'.
      WHEN OTHERS.
        LS_ZFIT078_HEAD-INVOICE_TYPE = LS_INVOICES-INVOICE_TYPE.
    ENDCASE.

    CONDENSE LS_INVOICES-SPECIAL_INVOICE_TYPE.
    CASE LS_INVOICES-SPECIAL_INVOICE_TYPE.
      WHEN '01'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '成品油发票'.
      WHEN '02'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '稀土发票'.
      WHEN '03'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '机动车'.
      WHEN '04'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '货物运输服务发票'.
      WHEN '05'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '不动产销售服务发票'.
      WHEN '06'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '不动产经营租赁服务'.
      WHEN '07'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '代收车船税'.
      WHEN '08'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '通行费'.
      WHEN '09'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '旅客运输服务发票'.
      WHEN '10'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '医疗服务（住院）发票'.
      WHEN '11'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '医疗服务（门诊）发票'.
      WHEN '12'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '自产农产品销售发票'.
      WHEN '13'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '拖拉机和联合收割机发票'.
      WHEN '15'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '二手车'.
      WHEN '16'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '农产品收购发票'.
      WHEN '17'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '光伏收购发票'.
      WHEN '18'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '卷烟发票'.
      WHEN '19'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '出口发票'.
      WHEN '20'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '农产品'.
      WHEN '21'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '铁路电子客票'.
      WHEN '22'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '航空运输电子客票行程单'.
      WHEN '31'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '建安发票'.
      WHEN '32'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '房地产销售发票'.
      WHEN '103'.
        LS_ZFIT078_HEAD-SPECIAL_INVOICE_TYPE = LS_INVOICES-SPECIAL_INVOICE_TYPE && GC_CONCATE_CHAR && '建筑服务'.

      WHEN OTHERS.
    ENDCASE.

    "获取供应商号码
    SELECT SINGLE
      LIFNR
      INTO LS_ZFIT078_HEAD-LIFNR
      FROM LFA1
     WHERE STCD5 = LS_ZFIT078_HEAD-ZXFSH
       AND LOEVM = SPACE
       AND SPERR = SPACE
       AND SPERM = SPACE.

    "获取公司代码
    SELECT SINGLE
      BUKRS
      INTO LS_ZFIT078_HEAD-BUKRS
      FROM T001
     WHERE STCEG = LS_ZFIT078_HEAD-ZGFSH.

    IF LS_ZFIT078_HEAD-BUKRS IS INITIAL.
      LS_ZFIT078_HEAD-BUKRS = 'CN10'.
    ENDIF.
    LS_ZFIT078_HEAD-WAERS = 'CNY'.
    LS_ZFIT078_HEAD-CRNAM = SY-UNAME.
    LS_ZFIT078_HEAD-CRDAT = SY-DATUM.
    LS_ZFIT078_HEAD-CRTIM = SY-UZEIT.

    IF SY-BATCH IS INITIAL.
      LV_STR = LS_ZFIT078_HEAD-ZFPHM.
      LV_STR1 = LS_ZFIT078_HEAD-ZXFMC.
      CONDENSE:
        LV_STR,
        LV_STR1.

      WRITE: / '发票:', LV_STR, ', 来自供应商:', LV_STR1, '已成功获取'.
    ENDIF.

    APPEND LS_ZFIT078_HEAD TO LT_ZFIT078_HEAD.

    CLEAR LS_ZFIT078_HIS.
    LS_ZFIT078_HIS-ZFPHM = LS_ZFIT078_HEAD-ZFPHM.
    LS_ZFIT078_HIS-CHNAM = SY-UNAME.
    LS_ZFIT078_HIS-CHDAT = SY-DATUM.
    LS_ZFIT078_HIS-CHTIM = SY-UZEIT.
    LS_ZFIT078_HIS-ZFPZT = '10'.  "已接收
    APPEND LS_ZFIT078_HIS TO LT_ZFIT078_HIS.
  ENDLOOP.

  LOOP AT GT_INVOICE_LIST_HG INTO DATA(LS_INVOICE_LIST_HG).
    CLEAR LS_ZFIT078_HEAD.

    LS_ZFIT078_HEAD-ZFPHM = LS_INVOICE_LIST_HG-INVOICE_NUMBER.
    " 开票时间
    LS_ZFIT078_HEAD-ZKPSJ = LS_INVOICE_LIST_HG-INVOICE_TIME+0(4) &&
                         LS_INVOICE_LIST_HG-INVOICE_TIME+5(2) &&
                         LS_INVOICE_LIST_HG-INVOICE_TIME+8(2).

    "销方名称(供应商描述)
    LS_ZFIT078_HEAD-ZXFMC = '中央金库'.
    " 销方税号
    " LS_ZFIT078_HEAD-ZXFSH = LS_INVOICE_LIST_HG-TAXNUM.

    "购方名称(公司描述)
    LS_ZFIT078_HEAD-ZGFMC = '华菱安赛乐米塔尔汽车板有限公司'.
*    " 购方税号
    LS_ZFIT078_HEAD-ZGFSH = LS_INVOICE_LIST_HG-TAXNUM.

    " 含税总金额、不含税总金额、合计税额
    TRY .
*        LS_ZFIT078_HEAD-ZHSJE  = LS_INVOICE_LIST_HG-VALID_TAX.
*        LS_ZFIT078_HEAD-ZBHSJE = LS_INVOICE_LIST_HG-VALID_TAX.
        LS_ZFIT078_HEAD-ZHJSE  = LS_INVOICE_LIST_HG-VALID_TAX.
        " LS_ZFIT078_HEAD-ZHJSE  = LS_INVOICES-TAX_AMOUNT.
      CATCH CX_ROOT.

    ENDTRY.

    LS_ZFIT078_HEAD-INVOICE_LINE = LS_INVOICE_LIST_HG-INVOICE_LINE && GC_CONCATE_CHAR && '海关缴款书'.

    CONDENSE LS_INVOICE_LIST_HG-MANAGEMENT_STATUS.
    CASE LS_INVOICE_LIST_HG-MANAGEMENT_STATUS.
      WHEN '0'.
        LS_ZFIT078_HEAD-INVOICE_STATUS = LS_INVOICE_LIST_HG-MANAGEMENT_STATUS && GC_CONCATE_CHAR && '正常'.
      WHEN '1'.
        LS_ZFIT078_HEAD-INVOICE_STATUS = LS_INVOICE_LIST_HG-MANAGEMENT_STATUS && GC_CONCATE_CHAR && '非正常(异常凭证)'.
      WHEN '2'.
        LS_ZFIT078_HEAD-INVOICE_STATUS = LS_INVOICE_LIST_HG-MANAGEMENT_STATUS && GC_CONCATE_CHAR && '疑似非正常'.
      WHEN OTHERS.
    ENDCASE.

    "获取供应商号码
    SELECT SINGLE
      LIFNR
      INTO LS_ZFIT078_HEAD-LIFNR
      FROM LFA1
     WHERE STCD5 = LS_ZFIT078_HEAD-ZXFSH
       AND LOEVM = SPACE
       AND SPERR = SPACE
       AND SPERM = SPACE.

    LS_ZFIT078_HEAD-BUKRS = 'CN10'.
    LS_ZFIT078_HEAD-WAERS = 'CNY'.
    LS_ZFIT078_HEAD-CRNAM = SY-UNAME.
    LS_ZFIT078_HEAD-CRDAT = SY-DATUM.
    LS_ZFIT078_HEAD-CRTIM = SY-UZEIT.

    IF SY-BATCH IS INITIAL.
      LV_STR = LS_ZFIT078_HEAD-ZFPHM.
      LV_STR1 = LS_ZFIT078_HEAD-ZXFMC.
      CONDENSE:
        LV_STR,
        LV_STR1.

      WRITE: / '海关缴款书:', LV_STR, ', 来自供应商:', LV_STR1, '已成功获取'.
    ENDIF.

    APPEND LS_ZFIT078_HEAD TO LT_ZFIT078_HEAD.

    CLEAR LS_ZFIT078_HIS.
    LS_ZFIT078_HIS-ZFPHM = LS_ZFIT078_HEAD-ZFPHM.
    LS_ZFIT078_HIS-CHNAM = SY-UNAME.
    LS_ZFIT078_HIS-CHDAT = SY-DATUM.
    LS_ZFIT078_HIS-CHTIM = SY-UZEIT.
    LS_ZFIT078_HIS-ZFPZT = '10'.  "已接收
    APPEND LS_ZFIT078_HIS TO LT_ZFIT078_HIS.
  ENDLOOP.

  "过滤掉历史记录中已经添加的发票
  IF LT_ZFIT078_HIS[] IS NOT INITIAL.
    SELECT *
      INTO TABLE @DATA(LT_ZFIT078_HIS_DB)
      FROM ZFIT078_HIS
       FOR ALL ENTRIES IN @LT_ZFIT078_HIS[]
     WHERE ZFPHM = @LT_ZFIT078_HIS-ZFPHM
       AND ZFPZT = '10'.  "已接收

    SORT LT_ZFIT078_HIS_DB BY ZFPHM.

    LOOP AT LT_ZFIT078_HIS INTO LS_ZFIT078_HIS.
      LV_TABIX = SY-TABIX.

      READ TABLE LT_ZFIT078_HIS_DB TRANSPORTING NO FIELDS
        WITH KEY ZFPHM = LS_ZFIT078_HIS-ZFPHM
        BINARY SEARCH.
      IF SY-SUBRC = 0.
        DELETE LT_ZFIT078_HIS INDEX LV_TABIX.
      ENDIF.
    ENDLOOP.
  ENDIF.

  IF LT_ZFIT078_HEAD[] IS NOT INITIAL.
    MODIFY ZFIT078_HEAD FROM TABLE LT_ZFIT078_HEAD.
    IF LT_ZFIT078_HIS[] IS NOT INITIAL.
      MODIFY ZFIT078_HIS FROM TABLE LT_ZFIT078_HIS.
    ENDIF.

    COMMIT WORK AND WAIT.
    WRITE: / '发票/海关缴款书数据已经成功获取'(T12).
  ENDIF.
ENDFORM.
